﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Internet_Deliver_Therapy.Models;
using System.Data.SqlClient;
using System.Configuration;

namespace Internet_Deliver_Therapy.Controllers.Patient
{
    public class PatientController
    {
        //Check if Patient is allowed to register
        public string checkWaitingList(string patient)
        {
            string psychiatrist = "";
            SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["IDTConnection"].ConnectionString);
            myconn.Open();
            SqlCommand cmd = new SqlCommand();
            SqlDataReader reader;

            cmd.Connection = myconn;
            String query = "SELECT Psychiatrist FROM PsychiatristWaitingList WHERE Patient = @Patient;";

            cmd.Parameters.AddWithValue("@Patient", patient);

            cmd.CommandText = query;

            try
            {
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        psychiatrist = reader.GetString(0);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            finally
            {
                myconn.Close();
            }

            return psychiatrist;
        }

        //Check if Patient is allowed to register
        public bool removePatientFromWaitingList(string patient)
        {
            bool deleted = true;
            SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["IDTConnection"].ConnectionString);
            myconn.Open();
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = myconn;
            String query = "DELETE FROM PsychiatristWaitingList WHERE Patient = @Patient;";

            cmd.Parameters.AddWithValue("@Patient", patient);

            cmd.CommandText = query;

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ex.ToString();
                deleted = false;
            }
            finally
            {
                myconn.Close();
            }

            return deleted;
        }

        //Values checked in consent form when Patient registers
        public bool createPatientConsentForm(string patient, string consentFormCheckedValues)
        {
            bool created = true;
            string kTPHPatient = "F";
            string nonKTPHPatient = "F";
            string diagnosed = "F";
            string eighteenNAbove = "F";
            string notSucidal = "F";
            string sucidal = "F";

            string[] consentFormValues = consentFormCheckedValues.Split(',');

            foreach (string value in consentFormValues)
            {
                if (value.Equals("KTPHPatient"))
                {
                    kTPHPatient = "T";
                }
                if (value.Equals("NonKTPHPatient"))
                {
                    nonKTPHPatient = "T";
                }
                if (value.Equals("Diagnosed"))
                {
                    diagnosed = "T";
                }
                if (value.Equals("EighteenNAbove"))
                {
                    eighteenNAbove = "T";
                }
                if (value.Equals("NotSucidal"))
                {
                    notSucidal = "T";
                }
                if (value.Equals("Sucidal"))
                {
                    sucidal = "T";
                }
            }

            SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["IDTConnection"].ConnectionString);

            myconn.Open();
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = myconn;

            String query = "INSERT INTO ConsentForms (NRIC, KTPHPatient, NonKTPHPatient, Diagnosed, EighteenNAbove, NotSucidal, Sucidal)"
                + "VALUES (@NRIC, @KTPHPatient, @NonKTPHPatient, @Diagnosed, @EighteenNAbove, @NotSucidal, @Sucidal)";

            cmd.Parameters.AddWithValue("@NRIC", patient);
            cmd.Parameters.AddWithValue("@KTPHPatient", kTPHPatient);
            cmd.Parameters.AddWithValue("@NonKTPHPatient", nonKTPHPatient);
            cmd.Parameters.AddWithValue("@Diagnosed", diagnosed);
            cmd.Parameters.AddWithValue("@EighteenNAbove", eighteenNAbove);
            cmd.Parameters.AddWithValue("@NotSucidal", notSucidal);
            cmd.Parameters.AddWithValue("@Sucidal", sucidal);

            cmd.CommandText = query;

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ex.ToString();
                created = false;
            }
            finally
            {
                myconn.Close();
            }

            return created;
        }

        //Return total number of active Patients
        public int totalActivePatients()
        {
            int totalActivePatients = 0;
            SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["IDTConnection"].ConnectionString);
            myconn.Open();
            SqlCommand cmd = new SqlCommand();
            SqlDataReader reader;

            cmd.Connection = myconn;
            String query = "SELECT COUNT(NRIC) As TotalActivePatients FROM Users WHERE AccountType = @AccountType AND "
                                + "Status = @Status;";

            cmd.Parameters.AddWithValue("@AccountType", "Patient");
            cmd.Parameters.AddWithValue("@Status", "T");

            cmd.CommandText = query;

            try
            {
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        totalActivePatients = reader.GetInt32(0);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            finally
            {
                myconn.Close();
            }

            return totalActivePatients;
        }
        
    }
}